package com.example.springmvc;

import com.example.springmvc.demos.web.PersonInfo;
import com.sun.deploy.net.HttpResponse;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

//可以修饰类
@RequestMapping("/User")

@RestController
public class UserController {
    @RequestMapping("/hello1")
    public String hello(){
        return "Hello Spring";
    }

    @RequestMapping("/r1")
    public String r1(String name){
        return "name:"+name;
    }

    @RequestMapping("/r2")
    //此处使用整数类型，必须传参数，不然会报500错误
    //所有对参数可能为空的类型，要使用包装类型
    public String r2(int age){
        return "AGE:"+age;
    }

    @RequestMapping("/r3")
    //改为Integer类型后，会直接显示null
    public String r3(Integer age){
        return "AGE:"+age;
    }

    @RequestMapping("/r4")
    public String r4(String name,String password){
        return "name:"+name+"  pw:"+password;
    }

    //参数的前后位置不影响获取
    @RequestMapping("/r5")
    public String r5(int id,String name,String password){
        return id+" "+name+" "+password;
    }

    //所有类都是Object的子类
    @RequestMapping("/r6")
    public Object r6(PersonInfo p){
        return p.toString ();
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam("time") String createtime){
        return "createtime:"+createtime;
    }

    //@RequestParam参数默认为必传，要想传空值required值修改为false
    @RequestMapping("/r8")
    public String r8(@RequestParam(value="time",required=false) String atime,@RequestParam(value="name",required=false) String bname){
        return "time:"+atime+" name:"+bname;
    }

    //传递数组
    @RequestMapping("/r9")
    public String r9(String[] array){
        //注意此处数组转为字符串的方法
        return Arrays.toString (array);
    }

    //传递集合  param:参数
    //@RequestParam此处是获取多个String类型的数据，共同组成一个list
    @RequestMapping("/r10")
    public String r10(@RequestParam List<String> Slist){
        return "size:"+Slist.size ()+" Slist:"+Slist;
    }

    //传递json数据
    @RequestMapping("/r11")
    //注解说明json数据转化为java
    public Object r11(@RequestBody PersonInfo person){
        return person.toString ();
    }


    //url绑定参数

    @RequestMapping("/r12/{name}/{password}")
    public Object r12(@PathVariable String name,@PathVariable String password){
        return "name:"+name+" password:"+password;
    }

    //Cookie的设置和获取
    //Cookie存在于网页中，通过网页进行获取
    @RequestMapping("/a1")
    public String a1(HttpServletRequest request, HttpServletResponse response){
        //获取所有cookie信息
        Cookie[] cookies=request.getCookies ();

        StringBuilder builder=new StringBuilder ();
        if(cookies!=null){
            for(Cookie ck:cookies){
                builder.append(ck.getName ()+":"+ck.getValue ());
            }
        }
        return "Cookie信息："+builder;
    }

    //校验的方式获取cookie
    //通过cookie的名字获取cookie
    //由于该方法通过Cookie的名字对Cookie进行获取，所以该方法一次只能获取一个Cookie
    @RequestMapping("/a2")
    public String a2(@CookieValue("zihao")String cookie){
            return "Cookie:"+cookie;
    }

    //session的存储和获取
    @RequestMapping("/Setse")
    public String Setse(HttpServletRequest request){
        HttpSession session = request.getSession ();
        if(session!=null){
            //此处是添加键值对，存储session信息
            session.setAttribute ("username","java");
        }
        return "储存成功";

    }


    //HttpSession getSession(boolean create) 参数如果为true,则当不存在会话时新建会话;
    // 参数如果为false,则当不存在会话时返回null

    @RequestMapping("/Getse")
    public String Getse(HttpServletRequest request){
        //不存在session时，不会自动创建
        HttpSession session = request.getSession (false);
        String username=null;
        if(session!=null&&session.getAttribute ("username")!=null){
            //getAttribute方法获取到的是Object类型的数据，此处要强转为String类型
            username= (String) session.getAttribute ("username");
        }
        return "username: "+username;
    }


    @RequestMapping("/Getse2")
    public String Getse2(@SessionAttribute(value="username",required = false) String username){
        return "username: "+username;
    }

    @RequestMapping("/Getse3")
    //没有传入任何参数，因为会根据http上下文自动提供session
    public String Getse3(HttpSession session){
        String username=(String) session.getAttribute ("username");
        return "username"+username;
    }


    //获取Header
    @RequestMapping("/a3")
    //虽然后面的一个参数没有用到，但仍然是十分必要的
    public String Hget(HttpServletRequest request,HttpServletResponse response){
        String UserAgent =(String)request.getHeader ("User-Agent");
        return "Username:"+UserAgent;
    }

    @RequestMapping("/a4")
    //对需求的参数进行赋值
    public String Hget2(@RequestHeader("User-Agent")String UserAgent){
        return "UserAgent:"+UserAgent;
    }







}
